package com.css.dzj.dzjwgr.modules.publicity.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjwgr.common.audit.service.SysAuditService;
import com.css.dzj.dzjwgr.common.util.BeanUtil;
import com.css.dzj.dzjwgr.common.util.Constant;
import com.css.dzj.dzjwgr.common.util.R;
import com.css.dzj.dzjwgr.common.util.ServiceUtil;
import com.css.dzj.dzjwgr.modules.publicity.entity.PublicityEntity;
import com.css.dzj.dzjwgr.modules.publicity.service.PublicityService;
import com.css.dzj.dzjwgr.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 公示通报Controller
 *
 * @author xgdou
 * @date 2020\3\5
 */
@RestController
@RequestMapping("/publicity")
public class PublicityController {
    @Autowired
    private PublicityService publicityService;
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private ServiceUtil serviceUtil;

    /**
     * 暂存
     */
    @PostMapping("/saveOnly")
    public R saveOnly(@RequestBody PublicityEntity publicityEntity) {
        String s = "";
        try {
            if (StringUtils.isEmpty(publicityEntity)) {
                return R.error("公示通报信息不能为空");
            }
            publicityService.addOnly(publicityEntity);
            s = JSON.toJSONString(publicityEntity);
            sysAuditService.saveSuccessAudit("公示通报暂存成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_ZCADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报暂存失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_ZCADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public R save(@RequestBody PublicityEntity publicityEntity) {
        String s = "";
        try {
            if (StringUtils.isEmpty(publicityEntity)) {
                return R.error("公示通报信息不能为空");
            }
            publicityService.add(publicityEntity);
            s = JSON.toJSONString(publicityEntity);
            sysAuditService.saveSuccessAudit("公示通报新增成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报新增失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody PublicityEntity publicityEntity) {
        String s = "";
        try {
            PublicityEntity old = publicityService.findById(publicityEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(publicityEntity, old);
            old.setUpdateTime(new Date());
            R r = serviceUtil.getUserInfoByRequest(request);
            if (r.getCode().equals("200")) {
                LoginUser userInfo = (LoginUser) r.get("data");
                old.setCreateUser(userInfo.getLoginName());
                old.setCreateUserUnit(userInfo.getOrgName());
            }
            publicityService.update(old);
            s = JSON.toJSONString(publicityEntity);
            sysAuditService.saveSuccessAudit("公示通报修改成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报修改失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(String id) {
        String s = "";
        try {
            if (publicityService.delete(id)) {
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("公示通报删除成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_DELETE, s, request);
                return R.ok("操作成功!");
            } else {
                return R.error("操作失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报删除失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 查看详情
     */
    @GetMapping("/info")
    public R info(@RequestParam String id) {
        PublicityEntity publicityEntity = publicityService.findById(id);
        String s = JSON.toJSONString(publicityEntity);
        sysAuditService.saveSuccessAudit("公示通报查看详情成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", publicityEntity);
    }


    /**
     * 多条件查询公示通报信息
     */
    @GetMapping("/publicityList")
    public R getXcbList(@RequestParam(name = "createUser", required = false) String createUser,
                        @RequestParam(name = "title", required = false) String title,
                        @RequestParam(name = "createUserUnit", required = false) String createUserUnit,
                        @RequestParam(name = "status", required = false) String status,
                        @RequestParam(name = "releaseStartTime", required = false) String releaseStartTime,
                        @RequestParam(name = "releaseEndTime", required = false) String releaseEndTime,
                        @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<PublicityEntity> publicityResults = publicityService.getPublicityList(createUser, title, createUserUnit, status, releaseStartTime, releaseEndTime, pageRequest);
            s = JSON.toJSONString(publicityResults);
            sysAuditService.saveSuccessAudit("多条件查询公示通报信息成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
            return R.ok().put("data", publicityResults);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("多条件查询公示通报信息失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 撤回公示通报信息
     */
    @PostMapping("/recall")
    public R recall(@RequestBody PublicityEntity publicityEntity) {
        String s = "";
        try {
            if (!"1".equals(publicityEntity.getStatus())) {
                return R.error("未发布状态，不能撤销!");
            }
            PublicityEntity old = publicityService.findById(publicityEntity.getId());
            old.setStatus("2");
            old.setUpdateTime(new Date());
            publicityService.update(old);
            s = JSON.toJSONString(old);
            sysAuditService.saveSuccessAudit("撤回公示通报信息成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_RECALL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("撤回公示通报信息失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_RECALL, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 发布公示通报信息
     */
    @PostMapping("/release")
    public R release(@RequestBody PublicityEntity publicityEntity) {
        String s = "";
        try {
            PublicityEntity old = publicityService.findById(publicityEntity.getId());
            old.setStatus("1");
            old.setReleaseTime(new Date());
            publicityService.update(old);
            s = JSON.toJSONString(old);
            sysAuditService.saveSuccessAudit("发布公示通报信息成功", Constant.AUDIT_MODULE_PUBLICITY, Constant.PT, Constant.AUDIT_TYPE_RELEASE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("发布公示通报信息失败", Constant.AUDIT_MODULE_PUBLICITY, Constant.YZ, Constant.AUDIT_TYPE_RELEASE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件分页查询外国人来华公示通报
     *
     * @param title
     * @param publicType
     * @param isPublic
     * @return listPublicity
     */
    @GetMapping("/listPublicity")
    public List listPublicity(@RequestParam String title, @RequestParam String publicType, @RequestParam String isPublic) {
        String s = "";
        try {
            List<PublicityEntity> list = publicityService.listPublicity(title, publicType, isPublic);
            return list;
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }

    }

    /**
     * 查询所有外国人来回公示通报信息
     *
     * @param publicType
     * @param isPublic
     * @return queryAllPublicity
     */
    @GetMapping("/queryAllPublicity")
    public List queryAllPublicity(@RequestParam String publicType, @RequestParam String isPublic) {
        String s = "";
        try {
            List<PublicityEntity> list = publicityService.queryAllPublicity(publicType, isPublic);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询公示通报信息详情
     *
     * @param id
     * @return PublicityEntity
     */
    @GetMapping("/interfaceinfo")
    public PublicityEntity interfaceinfo(@RequestParam String id) {
        String s = "";
        try {
            PublicityEntity publicityEntity = publicityService.findById(id);
            return publicityEntity;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
